package com.zhiwang.planet.ui.guessboard

import androidx.appcompat.widget.AppCompatTextView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.model.entity.RankItem
import com.zhiwang.planet.widget.RankView
import com.makeramen.roundedimageview.RoundedImageView

class RankAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<RankAdapter.BaseViewHolder>() {

    val datas = arrayListOf<RankItem>()
    lateinit var layoutInflater: LayoutInflater

    fun setData(datas: List<RankItem>) {
        this.datas.clear()
        this.datas.addAll(datas)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        layoutInflater = LayoutInflater.from(parent.context)
        if (viewType == 1) return HeaderViewHolder(layoutInflater.inflate(R.layout.item_guess_rank_header, parent, false))
        //view type 2
        return ItemViewHolder(layoutInflater.inflate(R.layout.item_guess_rank, parent, false))
    }

    override fun getItemCount() = if (datas.isEmpty()) 0 else datas.size + 1

    override fun getItemViewType(position: Int): Int {
        if (position == 0)
            return 1
        return 2
    }

    override fun onBindViewHolder(viewHolder: BaseViewHolder, position: Int) {
        viewHolder.bind()
    }

    abstract class BaseViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {

        abstract fun bind()

    }

    class HeaderViewHolder(itemView: View) : BaseViewHolder(itemView) {

        override fun bind() {

        }
    }


    inner class ItemViewHolder(itemView: View) : BaseViewHolder(itemView) {

        var rankView: RankView
        var avatar: RoundedImageView
        var userName: AppCompatTextView
        var energy: AppCompatTextView
        var diamond: AppCompatTextView

        init {
            rankView = itemView.findViewById(R.id.rank)
            avatar = itemView.findViewById(R.id.userAvatar)
            userName = itemView.findViewById(R.id.userName)
            energy = itemView.findViewById(R.id.energy)
            diamond = itemView.findViewById(R.id.diamond)
        }

        override fun bind() {
            when (adapterPosition) {
                1 -> rankView.setImageResource(R.drawable.ic_rank_1)
                2 -> rankView.setImageResource(R.drawable.ic_rank_2)
                3 -> rankView.setImageResource(R.drawable.ic_rank_3)
                else -> rankView.setText(adapterPosition.toString())
            }

            if ((adapterPosition - 1) % 2 == 0) {
                itemView.setBackgroundResource(R.color.white)
            } else itemView.setBackgroundResource(R.color.color_rank_bg)

            userName.text = datas[adapterPosition - 1].username
            Glide.with(itemView.context)
                    .load(datas[adapterPosition - 1].avatar)
                    .into(avatar)
            energy.text = datas[adapterPosition - 1].profit
            diamond.text = datas[adapterPosition - 1].reward_text

        }

    }
}